import { createStore } from "redux";

export type State = {
  pages?: Array<any>;
};

type Action = {
  type: string;
  payload?: any;
};

const reducer = (state: State = {}, action: Action): State => {
  switch (action.type) {
    // 添加页面
    case "ADD_PAGE":
      if (!state.pages) {
        state.pages = [];
      }
      state.pages.push(action.payload);
      return state;
    case "UPDATE_PAGE":
      if (state.pages) {
        const {index,data} = action.payload;
        state.pages[index] = data; 
      }
      return state;
    case "REMOVE_PAGE":
      if (state.pages) {
        state.pages.splice(action.payload, 1);
      }
      return state;
    case "REMOVE_ALL":
      if(state.pages){
        state.pages.length = 0;
      }
      return state;
    case "REMOVE_OTHER":
      if(state.pages){
        state.pages.length = 0;
        state.pages[0] = action.payload;
      }
      return state;
    default:
      return state;
  }
};

const store = createStore<State, Action, unknown, unknown>(reducer);

export default store;
